package com.iot.inclinometer.ui.newproject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.iot.inclinometer.MainActivity;
import com.iot.inclinometer.R;
import com.iot.inclinometer.common.BaseFragment;
import com.iot.inclinometer.project.entity.BaseInfo;
import com.iot.inclinometer.project.service.Impl.ProjectServiceImpl;
import com.iot.inclinometer.project.service.ProjectService;
import com.iot.inclinometer.ui.measuredata.MeasureDataActivity;

import org.apache.commons.lang3.StringUtils;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import lombok.SneakyThrows;

@ContentView(R.layout.fragment_new)
public class NewProjectFragment extends BaseFragment {
    //工程名称
    @ViewInject(R.id.project_name_edit)
    EditText project_name_edit;

    @ViewInject(R.id.project_name_spinner)
    Spinner project_name_spinner;
    //工点名称
    @ViewInject(R.id.item_name_edit)
    EditText item_name_edit;
    //孔位
    @ViewInject(R.id.drill_num_edit)
    EditText drill_num_edit;
    //孔深
    @ViewInject(R.id.depth_edit)
    EditText depth_edit;
    //里程桩号
    @ViewInject(R.id.mark1)
    Spinner mark1;
    //里程桩号
    @ViewInject(R.id.mark2)
    EditText mark2;
    //里程桩号
    @ViewInject(R.id.mark5)
    EditText mark5;
    //里程桩号
    @ViewInject(R.id.mark3)
    Spinner mark3;
    //里程桩号
    @ViewInject(R.id.mark4)
    EditText mark4;
    //位置描述
    @ViewInject(R.id.loc_edit)
    EditText loc_edit;
    //方位角
    @ViewInject(R.id.azimuth_edit)
    EditText azimuth_edit;
    //x坐标
    @ViewInject(R.id.x_edit)
    EditText x_edit;
    //y坐标
    @ViewInject(R.id.y_edit)
    EditText y_edit;
    //保存
    @ViewInject(R.id.save_btn)
    Button save_btn;
    //校正监测
    @ViewInject(R.id.start_measure_btn)
    Button start_measure_btn;
    //实时监测
    @ViewInject(R.id.r_start_measure_btn)
    Button r_start_measure_btn;

    ProjectService projectService = new ProjectServiceImpl();

    @Override
    public void onViewCreated(View v, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(v, savedInstanceState);
        ((MainActivity)this.getActivity()).registerMyTouchListener(myTouchListener);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        /** 触摸事件的注销 */
        ((MainActivity)this.getActivity()).unRegisterMyTouchListener(myTouchListener);
    }

    /** 接收MainActivity的Touch回调的对象，重写其中的onTouchEvent函数 */
    MainActivity.MyTouchListener myTouchListener = new MainActivity.MyTouchListener(){
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            //处理手势事件（根据个人需要去返回和逻辑的处理）
            return NewProjectFragment.this.onTouchEvent(event);
        }
    };


    //点击空白处关闭软键盘
    public boolean onTouchEvent(MotionEvent event){
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            if(getActivity().getCurrentFocus() != null && getActivity().getCurrentFocus().getWindowToken() != null){
                InputMethodManager im = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                im.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return true;
    }

    //保存按钮方法
    @SneakyThrows
    @Event(value = R.id.save_btn)
    private void saveBtnOnClick(View v) {
        BaseInfo baseInfo = new BaseInfo();
        if(StringUtils.isBlank(project_name_edit.getText().toString().trim())
                ||StringUtils.isBlank(item_name_edit.getText().toString().trim())
                ||StringUtils.isBlank(drill_num_edit.getText().toString().trim())
                ||StringUtils.isBlank(depth_edit.getText().toString().trim())){
            Toast.makeText(getActivity(),"有输入项为空，请输入",Toast.LENGTH_SHORT).show();
            start_measure_btn.setEnabled(true);
            return;
        }else{
            String mark2String = mark2.getText().toString().trim();
            String mark5String = mark5.getText().toString().trim();
            if("".equals(mark2String)&&"".equals(mark5String)){
                mark2String="";
            }else{
                mark2String =mark2String+'+'+mark5String;
            }
            baseInfo.setProject_name(project_name_edit.getText().toString().trim())
                    .setItem_name(item_name_edit.getText().toString().trim())
                    .setDrill_num(drill_num_edit.getText().toString().trim())
                    .setDepth(depth_edit.getText().toString().trim())
                    .setMark1(mark1.getSelectedItem().toString().trim())
                    .setMark2(mark2String)
                    .setMark3(mark3.getSelectedItem().toString().trim())
                    .setMark4(mark4.getText().toString().trim())
                    .setLoc(loc_edit.getText().toString().trim())
                    .setX(x_edit.getText().toString().trim())
                    .setY(y_edit.getText().toString().trim())
                    .setAzimuth(azimuth_edit.getText().toString().trim());
            boolean success = projectService.addOne(baseInfo);
            if(success){
                Toast.makeText(getActivity(),"保存成功",Toast.LENGTH_SHORT).show();
                start_measure_btn.setEnabled(true);
            }else{
                start_measure_btn.setEnabled(false);
                Toast.makeText(getActivity(),"保存失败，已存在该孔位",Toast.LENGTH_SHORT).show();

            }

        }
    }

    //校正监测方法
    @SneakyThrows
    @Event(value = R.id.start_measure_btn)
    private void startMeasureBtnOnClick(View v) {
//        if(getActivity().getSharedPreferences("configuration",0).getString("Tclib","").equals(""))
//        {
//            //参数为空，弹出吐司，提示先设置参数
//            Toast.makeText(getActivity(), "请设置参数后，再进入监测！",
//                    Toast.LENGTH_SHORT).show();
//        }else if(baseId.equals("")||baseId.equals("false")){
//            Toast.makeText(getActivity(), "请先保存项目！",
//                    Toast.LENGTH_SHORT).show();
//        }else{
            Intent intent = new Intent();
            intent.putExtra("depth", depth_edit.getText().toString().trim());
            intent.putExtra("itemName", item_name_edit.getText().toString().trim());
            intent.putExtra("drillNum", drill_num_edit.getText().toString().trim());
            intent.putExtra("from", "new");
            intent.setClass(getActivity(), MeasureDataActivity.class);
            startActivity(intent);
//        }
    }

}